package net.htjs.juc.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 结果合并
 */
@Slf4j
public class Demo4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 有返回值的任务
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            log.info("异步任务执行");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello";
        });
        CompletableFuture<String> combined = future2.thenCombine(
                CompletableFuture.supplyAsync(() -> "另一个结果"),
                (r1, r2) -> r1 + r2
        );
        log.info("合并运算结果:{}", combined.get());
    }

}
